//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.friendship
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.4 关系
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoFriendship extends DoAPI
	{
		public function DoFriendship(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_FRIENDSHIP_CREATE:String = 'http://api.t.163.com/friendships/create.json';
		public static const CMD_FRIENDSHIP_CREATE:String = 'cmdFriendshipCreate';
		
		private static const URL_FRIENDSHIP_DESTROY:String = 'http://api.t.163.com/friendships/destroy.json';
		public static const CMD_FRIENDSHIP_DESTROY:String = 'cmdFriendshipDestroy';
		
		private static const URL_FRIENDSHIP_SHOW:String = 'http://api.t.163.com/friendships/show.json';
		public static const CMD_FRIENDSHIP_SHOW:String = 'cmdFriendshipShow';
		
		private static const URL_FRIENDS:String = 'http://api.t.163.com/statuses/friends.json';
		public static const CMD_GET_FRIENDS:String = 'cmdGetFriends';
		
		private static const URL_FOLLOWERS:String = 'http://api.t.163.com/statuses/followers.json';
		public static const CMD_GET_FOLLOWERS:String = 'cmdGetFollowers';
		
		private static const URL_FRIENDS_NAME:String = 'http://api.t.163.com/friends/names.json';
		public static const CMD_GET_FRIENDS_NAME:String = 'cmdGetFriendsName';
		
		private static const URL_FOLLOWERS_NAME:String = 'http://api.t.163.com/statuses/followers/names.json';
		public static const CMD_GET_FOLLOWERS_NAME:String = 'cmdGetFollowersName';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 关注指定用户
		 * 
		 * @param user_id 可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 */		
		public function createFriendship(user_id:String, screen_name:String):void
		{
			var paras:Array = [];
			if (user_id != null && user_id != '')
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null && screen_name != '')
				paras.push(new HttpParameter('screen_name', screen_name));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			paras = OauthUrlUtil.getOauthPostParas(URL_FRIENDSHIP_CREATE, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_FRIENDSHIP_CREATE, resultHandler, errorHandler, URLRequestMethod.POST, postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_FRIENDSHIP_CREATE, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_FRIENDSHIP_CREATE, params);
			}
		}
		
		/**
		 * 取消关注指定用户
		 * 
		 * @param user_id 可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 */		
		public function destroyFriendship(user_id:String, screen_name:String):void
		{
			var paras:Array = [];
			if (user_id != null && user_id != '')
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null && screen_name != '')
				paras.push(new HttpParameter('screen_name', screen_name));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			paras = OauthUrlUtil.getOauthPostParas(URL_FRIENDSHIP_DESTROY, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_FRIENDSHIP_DESTROY, resultHandler, errorHandler, URLRequestMethod.POST, postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_FRIENDSHIP_DESTROY, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_FRIENDSHIP_DESTROY, params);
			}
		}
		
		/**
		 * 获取两个用户的相互关注关系
		 * 
		 * @param source_id 可选参数，指定用户的userId
		 * @param source_screen_name 可选参数，指定用户的个性网址
		 * @param target_id 可选参数，指定用户的userId
		 * @param target_screen_name 可选参数，目标用户的个性网址
		 */		
		public function getFriendship(source_id:String, source_screen_name:String, target_id:String, target_screen_name:String):void
		{
			var paras:Array = [];
			if (source_id != null)
				paras.push(new HttpParameter('source_id', source_id));
			if (source_screen_name != null)
				paras.push(new HttpParameter('source_screen_name', source_screen_name));
			if (target_id != null)
				paras.push(new HttpParameter('target_id', target_id));
			if (target_screen_name != null)
				paras.push(new HttpParameter('target_screen_name', target_screen_name));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_FRIENDSHIP_SHOW, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_FRIENDSHIP_SHOW, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_FRIENDSHIP_SHOW, params);
			}
		}
		
		/**
		 * 获取指定用户的关注列表
		 * 
		 * @param user_id 可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 * @param cursor 可选参数，分页参数，单页只能包含30个关注列表
		 */		
		public function getFriends(user_id:String, screen_name:String, cursor:String):void
		{
			var paras:Array = [];
			if (user_id != null)
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null)
				paras.push(new HttpParameter('screen_name', screen_name));
			if (cursor != null)
				paras.push(new HttpParameter('cursor', cursor));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_FRIENDS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_FRIENDS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_FRIENDS, params);
			}
		}
		
		/**
		 * 获取指定用户的被关注列表
		 * 
		 * @param user_id 可选参数. 用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，该用户的个性网址，也可以传user_id
		 * @param cursor 可选参数，分页参数，单页只能包含30个关注列表
		 */		
		public function getFollowers(user_id:String, screen_name:String, cursor:String):void
		{
			var paras:Array = [];
			if (user_id != null)
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null)
				paras.push(new HttpParameter('screen_name', screen_name));
			if (cursor != null)
				paras.push(new HttpParameter('cursor', cursor));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_FOLLOWERS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_FOLLOWERS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_FOLLOWERS, params);
			}
		}
		
		/**
		 * 获取当前用户的关注人名字列表
		 */		
		public function getFriendsName():void
		{
			var paras:Array = [];
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_FRIENDS_NAME, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_FRIENDS_NAME, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_FRIENDS_NAME, params);
			}
		}
		
		/**
		 * 获取指定用户的被关注的人名字列表，可用于发私信查找用户
		 * 
		 * @param user_id 可选参数. 指定用户ID，主要是用来区分用户ID跟微博昵称相同产生歧义的情况
		 * @param screen_name 可选参数，指定该用户的个性网址，也可以传user_id
		 * @param cursor 可选参数，分页参数，返回cursor之后的结果，包括cursor，可与count参数配合使用
		 * @param count 可选参数，数量，默认为30
		 * @param matchkey 可选参数，按照拼音或汉字模糊匹配的key，默认为空串（不做过滤）
		 * @param show_img 可选参数，为true显示头像的图片，默认不显示
		 */		
		public function getFollowersName(user_id:String, screen_name:String, cursor:String, count:String, matchkey:String, show_img:String):void
		{
			var paras:Array = [];
			if (user_id != null)
				paras.push(new HttpParameter('user_id', user_id));
			if (screen_name != null)
				paras.push(new HttpParameter('screen_name', screen_name));
			if (cursor != null)
				paras.push(new HttpParameter('cursor', cursor));
			if (count != null)
				paras.push(new HttpParameter('count', count));
			if (matchkey != null)
				paras.push(new HttpParameter('matchkey', matchkey));
			if (show_img != null)
				paras.push(new HttpParameter('show_img', show_img));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_FOLLOWERS_NAME, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_GET_FOLLOWERS_NAME, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_GET_FOLLOWERS_NAME, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			// decode json data result
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			// decode json data error
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
		
	}
}